// 云函数入口文件
const cloud = require('wx-server-sdk')
const cheerio =require('cheerio')//操作html模块
let charset =require('superagent-charset')//解决乱码
let superagent=require('superagent')//发起请求
const { load } = require('cheerio')
charset(superagent)//发起请求html通常乱码现在加一个解决乱码

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  let serverUrl ='https://m.hiyd.com//dongzuo/?equipment=10'
  const result =await superagent.get(serverUrl).charset('utf-8');
  const data =result.text || '';//没拿到返回一个空字符串
  const $ =cheerio.load(data)
  
  let keepList = $('.main-list').find('li')
  let keepData = []
  for(let i =0 ;i<keepList.length;i++){
    obj ={}
    let childData = []
    let childDom=$(keepList[i]).find('.item-cont').find('p').find('em')
       let childobj={}
      childobj['dengji']=$(childDom[0]).text()
      childData.push(childobj)
    obj['url']=$(keepList[i]).find('a').attr('href')
    obj['imgurl'] = $(keepList[i]).find('.item-pic').find('img').attr('src')
    obj['name']=$(keepList[i]).find('.item-cont').find('h3').text()
    obj['dengjiData'] = childData

    keepData.push(obj)
  }

  return {
    keepData
  }

}
